'''
Created on Jul 14, 2012

@author: charlie
'''

tbox = 'blank'

import pygame
import csv
import player
import move
import random

pokedex=[]
IV=31
EV=0


class Pokemon:
    def __init__ (self, ID=37, level=100, nature=1, initialize=0):
        if(initialize == 1):
            data=csv.reader(open('..\data\pokemon.csv'), delimiter=',', quotechar='/')
            for row in data:
                pokedex.append(row)
                print ('loading ' + pokedex[-1][0] +'-' + pokedex[-1][1])
        elif(type(ID) == int):
            self.ID=ID
            self.level=level
            self.nature=nature
            self.status=['none', 'none']
            self.Cstatus=['none', 'none']
            self.confcount=[0]
            self.BPcount=[0]
            self.sFullCount=[]
            self.Scount=[0]
            self.sleepytime=[0]
            self.HP_stat=[0,0]
            self.attack_stat=[0,0]
            self.defense_stat=[0,0]
            self.SPattack_stat=[0,0]
            self.SPdefense_stat=[0,0]
            self.speed_stat=[0,0]
            self.statMod1=0
            self.statMod2=0
            self.statMod3=0
            self.statMod4=0
            self.statMod5=0
            self.curse=False
            self.turnskip1=False
            self.status_check()
            self.Cstatus_check()
            self.move()
            self.namer()
            self.HP()
            self.attack()
            self.defense()
            self.SPattack()
            self.SPdefense()
            self.speed()
            self.type1()
            self.type2()
        else:
            self.ID=int(ID[0])
            self.level=level
            self.nature=nature
            self.status=['none', 'none']
            self.Cstatus=['none', 'none']
            self.confcount=[0]
            self.BPcount=[0]
            self.sFullCount=[]
            self.Scount=[0]
            self.sleepytime=[0]
            self.HP_stat=[0,0]
            self.attack_stat=[0,0]
            self.defense_stat=[0,0]
            self.SPattack_stat=[0,0]
            self.SPdefense_stat=[0,0]
            self.speed_stat=[0,0]
            self.statMod1=0
            self.statMod2=0
            self.statMod3=0
            self.statMod4=0
            self.statMod5=0
            self.curse=False
            self.turnskip1=False
            self.status_check()
            self.Cstatus_check()
            self.m1=move.Move(int(ID[1]))
            self.m2=move.Move(int(ID[2]))
            self.m3=move.Move(int(ID[3]))
            self.m4=move.Move(int(ID[4]))
            self.move=[self.m1, self.m2, self.m3, self.m4]
            self.namer()
            self.HP()
            self.attack()
            self.defense()
            self.SPattack()
            self.SPdefense()
            self.speed()
            self.type1()
            self.type2()
        
    def namer(self):
        self.name=pokedex[self.ID][1]
        return self.name
        
    def HP (self):
        HitP=int(((IV+(2*int(pokedex[self.ID][2]))+(EV/4)+100)*self.level/100)+10)
        self.HP_stat=[HitP, HitP]
        return self.HP_stat
    
    def attack (self):
        att=int((((IV+(2*int(pokedex[self.ID][3]))+(EV/4))*self.level)/100)+5)
        self.attack_stat=[att, att]
        return self.attack_stat
    
    def defense (self):
        DEF=int((((IV+(2*int(pokedex[self.ID][4]))+(EV/4))*self.level)/100)+5)
        self.defense_stat=[DEF, DEF]
        return self.defense_stat
    
    def SPattack (self):
        SPatt=int((((IV+(2*int(pokedex[self.ID][5]))+(EV/4))*self.level)/100)+5)
        self.SPattack_stat=[SPatt, SPatt]
        return self.SPattack_stat
    
    def SPdefense (self):
        SPdef=int((((IV+(2*int(pokedex[self.ID][6]))+(EV/4))*self.level)/100)+5)
        self.SPdefense_stat=[SPdef,SPdef]
        return self.SPdefense_stat
    
    def speed (self):
        spd=int((((IV+(2*int(pokedex[self.ID][7]))+(EV/4))*self.level)/100)+5)
        self.speed_stat=[spd, spd]
        return self.speed_stat
            
    def type1 (self):
        self.type1=int(pokedex[self.ID][8])
        return self.type1
    
    def type2 (self):
        self.type2=int(pokedex[self.ID][9])
        return self.type2
    
    def Cstatus_check (self):
        if self.Cstatus[-1] == 'conf':
            self.confcount.append(confcount[-1]+1)
            if 0<self.confcount[-1]<4:
                ra=random.randrange(0,2)
                if ra==0:
                    self.Cstatus.append('none')
                    tbox.addMessage(12, self.name)
                    return
            elif self.confcount >=4:
                self.Cstatus.append('none')
                tbox.addMessage(12, self.name)
                return
        else:
            return
        
    def status_check (self):
        if self.status[-2] == 'paralyz':
            self.speed_stat[0]=self.speed_stat[1]
        elif self.status[-2] == 'burn':
            self.attack_stat[0]=self.attack_stat[1]
        if self.status[-1]=='sleep':
            self.Scount.append(self.Scount[-1]+1)
            if self.sleepytime[-1] == 0:
                self.sFullCount.append(random.randrange(1,4))
                self.sleepytime.append(1)
            elif self.Scount[-1] < self.sFullCount[-1]:
                return
            elif self.Scount[-1]==self.sFullCount[-1]:
                self.status.append('none')
                tbox.addMessage(18, self.name)
                print('you woke up!')
                self.sleepytime.append(0)
        elif self.status[-1] == 'burn':
            self.attack_stat[0]=((self.attack_stat[1])//2)
            self.HP_stat[0]-=((self.HP_stat[1])//8)
            tbox.addMessage(14, self.name)
            print('damage taken. ' + str(self.HP_stat[0]) +'/'+ str(self.HP_stat[1]))
            return 'burn'
        elif self.status[-1] == 'poison':
            self.HP_stat[0]-=((self.HP_stat[1])//8)
            tbox.addMessage(15, self.name)
            return 'poison'
        elif self.status[-1] == 'bad_poison':
            BPcount.append(BPcount[-1]+1)
            self.HP_stat[0]-=((self.BPcount[-1])*(self.HP_stat[1])//16)
            tbox.addMessage(15, self.name)
            return 'bad poison'
        elif self.status[-1] == "frozen":
            ran=random.randrange(0,6)
            if ran == 3:
                status.append('none')
                tbox.addMessage(20, self.name)
                return 'none'
            return'frozen'
        elif self.status[-1] == 'paralyz':
            self.speed_stat[0]=((self.speed_stat[1])//4)
            return 'paralyz' 
        elif self.curse == True:
            self.HP_stat[0]-=((self.HP_stat[1])//4)
            tbox.addMessage(30, self.name + ' loses HP from it\'s curse')
        else:
            return 'none'
         
    def move (self):
        self.m1=move.Move(36)
        self.m2=move.Move(504)
        self.m3=move.Move(153)
        self.m4=move.Move(32)
        self.move=[self.m1, self.m2, self.m3, self.m4]
        return
    
    def checkAlive(self):
        if self.HP_stat[0]<=0 and self.status[-1] != 'fainted':
            self.status.append('fainted')
            tbox.addMessage(30, self.name + ' has fainted')
            print (self.status)
        return(self.HP_stat[0] > 0)
    
    def reset (self):
        self.status=['none', 'none']
        self.Cstatus=['none', 'none']
        self.confcount=[0]
        self.BPcount=[0]
        self.sFullCount=[]
        self.Scount=[0]
        self.sleepytime=[0]
        self.HP_stat[0]=self.HP_stat[1]
        self.attack_stat[0]=self.attack_stat[1]
        self.defense_stat[0]=self.defense_stat[1]
        self.SPattack_stat[0]=self.SPattack_stat[1]
        self.SPdefense_stat[0]=self.SPdefense_stat[1]
        self.speed_stat[0]=self.speed_stat[1]
        self.statMod1=0
        self.statMod2=0
        self.statMod3=0
        self.statMod4=0
        self.statMod5=0
        self.curse=False
        self.turnskip1=False
        self.m1.pp[0]=self.m1.pp[1]
        self.m2.pp[0]=self.m2.pp[1]
        self.m3.pp[0]=self.m3.pp[1]
        self.m4.pp[0]=self.m4.pp[1]